<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { userPageQuery, userQueryAll } from '@/api/user.js';
import type { ComponentSize } from 'element-plus';
import { CirclePlus, Delete, EditPen } from '@element-plus/icons-vue';
const options = [
  {
    value: '1',
    label: '女',
  },
  {
    value: '0',
    label: '男',
  },
  {
    value: '',
    label: '未知',
  }
]
const size = ref<ComponentSize>('default');
const background = ref(false);
const disabled = ref(false);
const total = ref(10);
const searchModel = ref({
  username: '',
  phone: '',
  sex: '',
  pageNo: 1,
  pageSize: 5,
});

const userList = ref([]);

// 定义函数获取用户列表
const userquerall = () => {
  userQueryAll()
    .then((response) => {
      userList.value = response.data.data; // 更新用户列表
      console.log('User List:', userList.value);
    })
    .catch((error) => {
      console.error('Error fetching data:', error);
    });
};

// 定义函数获取用户分页查询列表
const userpagequery = async(params) => {
  // 确保传递当前最新的查询参数
  try {
    const response = await userPageQuery(params);
    userList.value = response.data.data; // 更新用户列表
    total.value = response.data.total; // 更新总数
    console.log('User List:', userList.value);
    console.log('Total:', total.value);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
};

// 格式化性别
const formatGender = (value) => {
  return value.sex === 0 ? '男' : '女';
};

// 组件挂载时调用数据
onMounted(() => {
  userpagequery(searchModel.value); // 传入 searchModel 的深拷贝
});

// 处理每页显示数量变化
const handleSizeChange = (val: number) => {
  searchModel.value.pageSize = val; // 更新 pageSize
  userpagequery(searchModel.value); // 重新查询，传入最新参数
};

// 处理当前页变化
const handleCurrentChange = (val: number) => {
  searchModel.value.pageNo = val; // 更新 pageNo
  userpagequery(searchModel.value); // 重新查询，传入最新参数
};

const cleandata = () => {
  searchModel.value = { // 重置为默认值
    username: '',
    phone: '',
    sex: '',
    pageNo: 1,
    pageSize: 5,
  }
  userpagequery(searchModel.value)
}
</script>
<style scoped>
.search-container {
  display: flex;
  gap: 10px; /* 选填：设置元素之间的间距 */
  align-items: center; /* 选填：垂直居中对齐 */
  padding: 10px 10px;
}
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
.layout-container-demo .el-main {
  padding: 0;
  flex: 1; 
}

</style>
<template>
      <div class="search-container">
    <el-input v-model="searchModel.username" style="width: 240px" placeholder="请输入姓名" />
    <el-input v-model="searchModel.phone" style="width: 240px" placeholder="请输入电话" />
    <el-select v-model="searchModel.sex" placeholder="请输入性别" style="width: 240px">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value"
    />
  </el-select>
    <el-button type="primary" @click="userpagequery(searchModel)">查询</el-button>
    <el-button type="warning" @click="cleandata()">重置</el-button>
    <div class="el-buttonone">
      <el-button type="primary"><el-icon><CirclePlus /></el-icon>新增</el-button>
      <el-button type="danger"><el-icon><Delete /></el-icon>批量删除</el-button>
    </div>

  </div>
      <el-main>
        <el-scrollbar>
          <el-table :data="userList">
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column prop="username" label="姓名" width="180" />
            <el-table-column prop="phone" label="电话" width="180" />
            <el-table-column prop="sex" label="性别" width="180" :formatter="formatGender" />
            <el-table-column prop="age" label="年龄" width="180" />
            <el-table-column label="操作">
              <el-button type="warning"><el-icon><EditPen /></el-icon>编辑</el-button>
              <el-button type="danger"><el-icon><Delete /></el-icon>删除</el-button>
            </el-table-column>
          </el-table>
        </el-scrollbar>
      </el-main>

      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchModel.pageNo"
          v-model:page-size="searchModel.pageSize"
          :page-sizes="[1, 2, 5, 10]"
          :size="size"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
</template>